home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9207.ARJ / 1007064A < prev    next >
Text File  |  1992-06-03  |  4KB  |  157 lines

  1. /***********************************************
  2.  *                                             *
  3.  *          "DON'T MESS WITH MARILYN"          *
  4.  *                                             *
  5.  *            MONTE CARLO SIMULATION           *
  6.  *                                             *
  7.  * 02/21/91, M.E. Brandt, Ph.D.                *
  8.  *                                             *
  9.  ***********************************************/
  10.  
  11. #include <stdio.h>
  12.  
  13.  
  14. #define BIT_MASK      7
  15. #define NO_DOORS      3.0
  16. #define RANDFACTOR    (32768.0 * (1.0/NO_DOORS))
  17.  
  18.  
  19. main()
  20. {
  21.  
  22.     int i, n, car, choice, open, count;
  23.     
  24.     unsigned char u[2], doors, noprize, mask, not_open,
  25.                   not_choice; 
  26.     
  27.     double prob;
  28.     
  29.     
  30.     srand(1);
  31.  
  32.     do {
  33.     
  34.         fprintf(stderr, 
  35.          "\nHow many games of not-switching, and switching\
  36.  would you like?: ");
  37.         scanf("%d", &n);
  38.  
  39.         printf("\n\nNumber of trials in this series is %d\n",
  40.                n);
  41.        
  42.  
  43.         /* LOOP IN WHICH CONTESTANT DOES NOT SWITCH */
  44.    
  45.         for(count=i=0; i<n; i++) {
  46.    
  47.             doors = 1;
  48.     
  49.             /* get a random number between 0 and 2 to hide 
  50.                car behind door # */
  51.     
  52.             car =  (int)((double)rand()/RANDFACTOR);
  53.     
  54.             doors <<= car;               /* status of doors */
  55.     
  56.             noprize = ~doors & BIT_MASK; /* doors with goats */
  57.         
  58.             /* contestant picks a door */
  59.         
  60.             choice =  (int)((double)rand()/RANDFACTOR);
  61.     
  62.             /* host reveals a door which is not 'choice' 
  63.                and not 'car' (a goat!) */
  64.     
  65.             mask = noprize & ((~(1 << choice)) & BIT_MASK);
  66.    
  67.             /* host always picks first one not satisfying 
  68.                condition */
  69.          
  70.             open = 0;
  71.             while(!(mask & 1)) {
  72.                mask >>= 1;
  73.                open++;
  74.             }
  75.     
  76.             if(choice == car) count++;
  77.     
  78.             /* since the contestant did not switch, 'choice' 
  79.                does not depend on 'open'! */
  80.     
  81.      
  82.         }
  83.     
  84.         prob = (double)count/(double)n;
  85.  
  86.         printf(
  87.          "\nPercentage of wins for non-switching is %.1f",
  88.                prob * 100.0);
  89.         
  90.  
  91.  
  92. /* LOOP IN WHICH CONTESTANT SWITCHES CHOICE */
  93.  
  94.        for(count=i=0; i<n; i++) {
  95.    
  96.            doors = 1;
  97.     
  98.         /* get a random number between 0 and 2 to hide car 
  99.            behind door # */
  100.     
  101.            car =  (int)((double)rand()/RANDFACTOR);
  102.     
  103.            doors <<= car;               /* status of doors */
  104.     
  105.            noprize = ~doors & BIT_MASK; /* doors with goats */
  106.         
  107.           /* contestant picks a door */
  108.         
  109.            choice =  (int)((double)rand()/RANDFACTOR);
  110.     
  111.           /* host reveals a door which is not 'choice' and not
  112.              'car' */
  113.     
  114.            mask = noprize & ((~(1 << choice)) & BIT_MASK);
  115.     
  116.            open = 0;
  117.            while(!(mask & 1)) {
  118.               mask >>= 1;
  119.               open++;
  120.            }
  121.  
  122.  
  123.          /* now the contestant switches to door which is not 
  124.             'choice' and not 'open' */
  125.        
  126.            not_choice = ~(1 << choice) & BIT_MASK;
  127.            not_open   = ~(1 << open)   & BIT_MASK;
  128.        
  129.            choice = not_open & not_choice;        
  130.  
  131.  
  132.          /* if switched choice = original status of 'doors' 
  133.             it's a "win"! */
  134.       
  135.            if(choice == doors) count++;
  136.     
  137.      
  138.         }
  139.  
  140.     
  141.         prob = (double)count/(double)n;
  142.  
  143.         printf("\nPercentage of wins for switching is %.1f\n",
  144.                prob * 100.0);
  145.         printf("\n-----------------------------------------\
  146. -----\n");
  147.  
  148.         fprintf(stderr, "\nPlay Again?: ");
  149.         scanf("%s", u);
  150.     
  151.   }
  152.   while(toupper(*u) == 'Y');
  153.     
  154.  
  155. }
  156.     
  157.